package git.soulbgm.common.handler;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import git.soulbgm.pojo.AlarmRuleInfo;
import git.soulbgm.pojo.DictData;
import git.soulbgm.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 告警相关Excel字典参数处理程序
 *
 * @author SoulBGM
 * @date 2022/03/31 10:30
 */
@Component
public class AlarmExcelDictHandlerImpl implements IExcelDictHandler {

    @Autowired
    private DictDataService dictDataService;

    private static final String PREFIX = "alarm_rule_";

    @Override
    public String toName(String dict, Object obj, String name, Object value) {
        if ("事件编号".equals(name) && obj instanceof AlarmRuleInfo) {
            AlarmRuleInfo info = (AlarmRuleInfo) obj;
            List<DictData> childList = dictDataService.getType(PREFIX + info.getRuleType());
            return childList.stream().collect(Collectors.toMap(DictData::getDictValue, DictData::getDictLabel)).get(value.toString());
        }
        List<DictData> list = dictDataService.getType(dict);
        int prefixLength = PREFIX.length();
        return list.stream().collect(Collectors.toMap(o -> o.getDictValue().substring(prefixLength), DictData::getDictLabel)).get(value.toString());
    }

    @Override
    public String toValue(String dict, Object obj, String name, Object value) {
        return null;
    }
}
